function SortByOrder(itemA, itemB) {
  return itemA.orderIndex - itemB.orderIndex
}
/**
 * 将传来的路由数据进行过滤
 * @param {*} resource
 * @returns
 */
export const getRoutes = (resource) => {
  const RouterMap = []
  resource.sort(SortByOrder)
  for (const pack of resource) {
    const route = []
    if (pack.resourceType !== 0 && pack.parentId !== '-1') { continue }
    if (pack.api[0] === '/')pack.api = pack.api.substr(1)
    route.path = pack.api
    route.name = pack.name
    // route.component = () => import('@/views/RoleManage/index.vue')
    route.meta = {}
    route.meta.title = pack.name
    route.meta.icon = pack.openImg
    // route.meta.index = pack.orderIndex
    // route.status = pack.status
    if (pack.resourceVOs) {
      route.children = getRoutes(pack.resourceVOs)
    } else {
      route.children = []
    }
    RouterMap.push(route)
  }

  return RouterMap
}
// 自定义多维数组包特定对象转一维数组过滤children，后端若返回一维数组则直接交由filterAsyncRoutes处理
export function MultidimensionalToOnedimensional(routesMap) {
  const filterRoutesMap = []
  !(function fn(routesMap) {
    routesMap.forEach(route => {
      const tmp = {}
      for (const key in route) {
        if (Object.hasOwnProperty.call(route, key)) {
          if (key !== 'children') {
            tmp[key] = route[key]
          } else if (key === 'children') {
            fn(route[key])
          }
        }
      }
      filterRoutesMap.push(tmp)
    })
  }(routesMap))
  return filterRoutesMap
}

// 路由索引匹配
export function filterAsyncRoutes(routes, filterRoutesMap) {
  const accessedRoutes = []
  debugger
  routes.forEach(route => {
    const tmp = {}
    if (filterRoutesMap.some(a => a.name === route.name)) {
      for (const key in route) {
        if (Object.hasOwnProperty.call(route, key)) {
          if (key !== 'children') {
            tmp[key] = route[key]
          } else if (key === 'children') {
            const tmpC = filterAsyncRoutes(route.children, filterRoutesMap);
            (tmpC.length > 0) && (tmp.children = tmpC)
          }
        }
      }
    }
    tmp.path && accessedRoutes.push(tmp)
  })

  return accessedRoutes
}
